<?php
namespace App\Http\Controllers\Admin;

use App\Services\Admin\SceneCntService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class SceneCntController extends BaseController
{
    //场景内容

    public function __construct(SceneCntService $service)
    {
        $this->db = $service;
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->post('pageStart', 0);
        $pages['limit'] = $request->post('pageLimit', 10);
        $scene_id = $request->post('scene_id', 0);
        $rst = $this->db->getIndex($pages, $scene_id);
        if ($rst['code']!=0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0, '', $rst['data'], $rst['total']);
    }

    //上传图片
    public function uploadImg(Request $request)
    {
        $img = $request->post('img', '');
        if (!$img) {
            return $this->toJson(-2, '图片必须的');
        }
        $fpath = Upload::getPathByBase64($img);
        $res = $this->db->addFile(1, $fpath);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $fpath);
    }

    //上传视频
    public function uploadVideo(Request $request)
    {
        ini_set('upload_max_filesize', '100M');
        ini_set('post_max_size', '100M');
        $upload = Upload::uploadVideo($request, 1, 1, time());
        if ($upload['code'] < 0) {
            return $this->toJson(-2, $upload['msg']);
        }
        $fpath = $upload['data'];
        $res = $this->db->addFile(2, $fpath);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $fpath);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['scene_id'] = $request->post('scene_id', 0);
        $pas['type'] = $request->post('type', 0);
        $title = $request->post('title', '');
        $img = $request->post('img', '');
        $video = $request->post('video', '');
        if (!$pas['scene_id']) {
            return $this->toJson(-2, '场景必须的');
        }
        if ($pas['type'] == 1) {
            if (!$title) {
                return $this->toJson(-2, '文字必须的');
            }
            $pas['cnt'] = $title;
        } else if ($pas['type'] == 2) {
            if (!$img) {
                return $this->toJson(-2, '图片必须的');
            }
            $pas['cnt'] = $img;
        } else if ($pas['type'] == 3) {
            if (!$video) {
                return $this->toJson(-2, '视频必须的');
            }
            $pas['cnt'] = $video;
        } else {
            return $this->toJson(-2, '类型必须的');
        }
        if ($id) {
            $res = $this->db->modify($id, $pas);
        } else {
            $res = $this->db->add($pas);
        }
        if ($res['code']!=0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        //字段验证
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => '场景ID必须的',
        ]);
        $id = $request->post('id', 0);
        $res = $this->db->del($id);
        if ($res['code']!=0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}